<template>
	<view class="container">
		<uni-forms class="server-form" :modelValue="formData" border>
			<uni-forms-item label="后端服务url:" label-width="100" autoHeight name="ip" label-align="left">
				<!-- <uni-easyinput type="textarea" :inputBorder="false" v-model="formData.ip" placeholder="" /> -->
				<u--textarea
					maxlength="1000"
					autoHeight
					v-model="formData.ip"
					border="none"
					placeholder="请输入"
				></u--textarea>
			</uni-forms-item>
			
			<view class="" @tap="showAccessMode">
				<uni-forms-item label="访问模式:" label-width="100" name="mode" label-align="left">
					<uni-easyinput type="text" :inputBorder="false" v-model="formData.mode" placeholder="" />
				</uni-forms-item>
			</view>
			<uni-forms-item>
				<button plain @click="submitForm" type="primary">确定</button>
			</uni-forms-item>
		</uni-forms>
		<!-- <button plain @click="checkNewVersion" type="primary">检查更新</button> -->
		<u-loading-icon text="加载中" :show="showloading" textSize="18"></u-loading-icon>
	</view>
</template>

<script>
import baseUrl, { API_IP } from '@/common/const/index';
import tool from '../../../tool/tool.js';
export default {
	data() {
		return {
			showloading: false,
			formData: {
				ip: '',
				mode: ''
			},
			modeList:['正式环境', '开发环境', '企微开发环境', '内网穿透'],
			baseApis:[
				'https://www.ganfeng-li.com:8443/genexusMes2',
				'http://127.0.0.1:81/Gx_adminJavaEnvironment',
				'https://www.ganfeng-li.com:8443/qwgenexusMes2',
				'http://i369d53836.oicp.vip:34739/Gx_adminJavaEnvironment'
			]
		};
	},
	onReady() {
		let showUrl = uni.getStorageSync('baseUrl');
		this.baseApis.map((item,index)=>{
			if(showUrl === item){
				this.formData.mode = this.modeList[index]
			}
		});
		this.formData.ip = showUrl;
	},
	methods: {
		showAccessMode(event) {
			uni.hideKeyboard()
			let _this = this;
			uni.showActionSheet({
				itemList: this.modeList,
				success: function (res) {
					let mode = _this.modeList[res.tapIndex];
					let showUrl = _this.baseApis[res.tapIndex];
					
					_this.formData.mode = mode;
					_this.formData.ip = showUrl;
					
					uni.setStorageSync('baseUrl', showUrl);
				},
				fail: function (res) {}
			});
		},
		submitForm() {
			this.$GC.API_IP = this.formData.ip;
			uni.setStorageSync('baseUrl', this.formData.ip);
			this.$tool.msg('服务器地址已更改！');

			/* this.showloading = true;
				let ipAndPort = `http://${this.formData.ip}:${this.formData.port}`;
				let url = `${ipAndPort}/uapws/rest/qcmobbus/ncLogin`;

				// 基本用法，注意：post的第三个参数才为配置项
				uni.$u.http.post(url, {}, {
					timeout: 5000,
				}).then(res => {
					this.showloading = false;
					uni.setStorageSync('request-url', ipAndPort);
					uni.showToast({
						icon: 'none',
						title: '请求成功！服务器地址已更改'
					})
				}).catch(err => {
					this.showloading = false;
					uni.showToast({
						icon: 'none',
						title: '网络连接失败！'
					})
				}) */
		},
		/**
		 * 检查是否有新版本APP
		 */
		checkNewVersion() {
			// 请求服务ip
			let request_url = uni.getStorageSync('request-url');
			let data = {
				appid: plus.runtime.appid,
				version: plus.runtime.version
			};
			// console.log("data: " + JSON.stringify(data));
			/* let data = {
					"appid": "__UNI__97DAEBE",
					"version": "1.0.0",
				}; */
			this.$api
				.checkNewVersion(data)
				.then((res) => {
					if (res.code == 0 && res.data && res.data.status == 1) {
						uni.showModal({
							//提醒用户更新
							title: '更新提示',
							content: res.data.note,
							success: (event) => {
								if (event.confirm) {
									uni.showLoading({
										title: '正在下载安装包，请稍后!'
									});
									var downloadTask = uni.downloadFile({
										url: `${request_url}/service/downloadQcApp`,
										success: (downloadResult) => {
											uni.hideLoading();
											if (downloadResult.statusCode === 200) {
												plus.runtime.install(
													downloadResult.tempFilePath,
													{
														force: false
													},
													function () {
														plus.runtime.restart();
														// plus.nativeUI.toast('更新成功');
													},
													function (e) {
														// plus.nativeUI.toast('更新失败');
													}
												);
											}
										},
										fail: (event) => {
											uni.hideLoading();
											plus.nativeUI.toast('安装包下载失败');
										}
									});
								}
							}
						});
					} else {
						plus.nativeUI.toast('没有最新版本');
					}
				})
				.catch((err) => {
					plus.nativeUI.toast('没有最新版本');
				});
		}
	}
};
</script>

<style>
.container {
	padding: 10px;
	font-family: 'PingFang SC Regular';
}

/deep/ .uni-forms-item__inner {
	padding-bottom: 10px;
}
</style>
